package com.photoeditor.demo.model.face.report.reportlogic;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

public class OneTimeVipHelper {
    private static OneTimeVipHelper mInstance;
    private int mAge;
    private List<Integer> mUnlockedAges = new ArrayList<>();
    private String mTemplateId;
    private List<String> mUnlockedTemplateIds = new ArrayList<>();
    private boolean mCanUseGenderSwitch = false;

    public static final String original = "original";

    public static OneTimeVipHelper getInstance() {
        if (mInstance == null) {
            mInstance = new OneTimeVipHelper();
        }
        return mInstance;
    }

    public void everyInit() {
        mAge = -1;
        mUnlockedAges.clear();
        mTemplateId = "";
        mUnlockedTemplateIds.clear();
        mCanUseGenderSwitch = false;
    }

    public void setAge(int age) {
        mAge = age;
    }

    public int getAge() {
        return mAge;
    }

    public int getLatestUnlockedAge() {
        if (mUnlockedAges.isEmpty()) {
            return -1;
        }
        return mUnlockedAges.get(mUnlockedAges.size() - 1);
    }

    public void unlockAge() {
        if (mAge != -1 && !mUnlockedAges.contains(mAge)) {
            mUnlockedAges.add(mAge);
        }
    }

    public boolean isAgeUnlocked() {
        return isAgeUnlocked(mAge);
    }

    public boolean isAgeUnlocked(int age) {
        if (age == -1 || age == 0) {
            //初始值 -1
            //选中原图 0
            return true;
        }
        if (mUnlockedAges.isEmpty()) {
            return false;
        }
        for (Integer a: mUnlockedAges) {
            if (age == a) {
                return true;
            }
        }
        return false;
    }

    public int getUnlockedAgeCount() {
        return mUnlockedAges.size();
    }

    public void setTemplateId(String templateId) {
        mTemplateId = templateId;
    }

    public String getLatestUnlockedTid() {
        if (mUnlockedTemplateIds.isEmpty()) {
            return "";
        }
        return mUnlockedTemplateIds.get(mUnlockedTemplateIds.size() - 1);
    }

    public void unlockTemplateId() {
        if (!TextUtils.isEmpty(mTemplateId) && !mUnlockedTemplateIds.contains(mTemplateId)) {
            mUnlockedTemplateIds.add(mTemplateId);
        }
    }

    public void forceUnlockTemplate(String templateId) {
        mUnlockedTemplateIds.add(templateId);
    }

    public boolean isTemplateUnlocked() {
        return isTemplateUnlocked(mTemplateId);
    }

    public boolean isTemplateUnlocked(String templateId) {
        if (TextUtils.isEmpty(templateId) || TextUtils.equals(templateId, original)) {
            //初始值 ""
            //选中原图 "original"
            return true;
        }
        if (mUnlockedTemplateIds.isEmpty()) {
            return false;
        }
        for (String id: mUnlockedTemplateIds) {
            if (TextUtils.equals(templateId, id)) {
                return true;
            }
        }
        return false;
    }

    public int getUnlockedTemplateCount() {
        return mUnlockedTemplateIds.size();
    }

    public void unlockGenderSwitch() {
        mCanUseGenderSwitch = true;
    }

    public boolean isGenderSwitchUnlocked() {
        return mCanUseGenderSwitch;
    }
}
